{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "15fa8a2f",
   "metadata": {},
   "source": [
    "# 字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2508c92",
   "metadata": {},
   "source": [
    "## 字符串\n",
    "python 中的字符串被单引号或双引号包围。\n",
    "\n",
    "'hello'和\"hello\"是一样的。\n",
    "\n",
    "您可以使用 print() 函数显示字符串文字："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ac622691",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello\n",
      "Hello\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello\")\n",
    "print('Hello')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73a1641e",
   "metadata": {},
   "source": [
    "## 将字符串分配给变量\n",
    "将字符串分配给变量是通过变量名后跟等号和字符串来完成的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "403ee55d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello\n"
     ]
    }
   ],
   "source": [
    "a = \"Hello\"\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29436d9e",
   "metadata": {},
   "source": [
    "## 多行字符串\n",
    "您可以使用三个引号将多行字符串分配给变量："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "494c88ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lorem ipsum dolor sit amet,\n",
      "consectetur adipiscing elit,\n",
      "sed do eiusmod tempor incididunt\n",
      "ut labore et dolore magna aliqua.\n",
      "Lorem ipsum dolor sit amet,\n",
      "consectetur adipiscing elit,\n",
      "sed do eiusmod tempor incididunt\n",
      "ut labore et dolore magna aliqua.\n"
     ]
    }
   ],
   "source": [
    "a = \"\"\"Lorem ipsum dolor sit amet,\n",
    "consectetur adipiscing elit,\n",
    "sed do eiusmod tempor incididunt\n",
    "ut labore et dolore magna aliqua.\"\"\"\n",
    "b = '''Lorem ipsum dolor sit amet,\n",
    "consectetur adipiscing elit,\n",
    "sed do eiusmod tempor incididunt\n",
    "ut labore et dolore magna aliqua.'''\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "63dab00f",
   "metadata": {},
   "source": [
    "## 字符串是数组\n",
    "像许多其他流行的编程语言一样，Python 中的字符串是表示 unicode 字符的字节数组。\n",
    "\n",
    "但是，Python 没有字符数据类型，单个字符只是一个长度为 1 的字符串。\n",
    "\n",
    "方括号可用于访问字符串的元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bfbdc66b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "e\n"
     ]
    }
   ],
   "source": [
    "a = \"Hello, World!\"\n",
    "print(a[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34a7c056",
   "metadata": {},
   "source": [
    "## 遍历字符串\n",
    "由于字符串是数组，我们可以使用 for 循环遍历字符串中的字符。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "90b1b63b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b\n",
      "a\n",
      "n\n",
      "a\n",
      "n\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "for x in \"banana\":\n",
    "  print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a3c3f92",
   "metadata": {},
   "source": [
    "## 字符串长度\n",
    "要获取字符串的长度，请使用 len() 函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0d7df37c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13\n"
     ]
    }
   ],
   "source": [
    "a = \"Hello, World!\"\n",
    "print(len(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5611cdd",
   "metadata": {},
   "source": [
    "## 检查字符串\n",
    "要检查字符串中是否存在某个短语或字符，我们可以使用关键字 in。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "075dc99f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "txt = \"The best things in life are free!\"\n",
    "print(\"free\" in txt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6e0a8264",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Yes, 'free' is present.\n"
     ]
    }
   ],
   "source": [
    "txt = \"The best things in life are free!\"\n",
    "if \"free\" in txt:\n",
    "  print(\"Yes, 'free' is present.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "afa0a8c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "txt = \"The best things in life are free!\"\n",
    "print(\"expensive\" not in txt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2ae78d43",
   "metadata": {},
   "source": [
    "# 字符串切片"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4fea772",
   "metadata": {},
   "source": [
    "## 切片\n",
    "您可以使用切片语法返回一系列字符。\n",
    "\n",
    "指定开始索引和结束索引，以冒号分隔，以返回字符串的一部分。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "17ba7194",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "llo\n"
     ]
    }
   ],
   "source": [
    "b = \"Hello, World!\"\n",
    "print(b[2:5])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e92c53e",
   "metadata": {},
   "source": [
    "## 从头开始切片\n",
    "通过省略开始索引，范围将从第一个字符开始："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "08fa63fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello\n"
     ]
    }
   ],
   "source": [
    "b = \"Hello, World!\"\n",
    "print(b[:5])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71772200",
   "metadata": {},
   "source": [
    "## 切到最后\n",
    "通过省略结束索引，范围将到最后："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c4c99da4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "llo, World!\n"
     ]
    }
   ],
   "source": [
    "b = \"Hello, World!\"\n",
    "print(b[2:])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae83a43e",
   "metadata": {},
   "source": [
    "## 负索引\n",
    "使用负索引从字符串末尾开始切片："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c25370a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "orl\n"
     ]
    }
   ],
   "source": [
    "b = \"Hello, World!\"\n",
    "print(b[-5:-2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97d93230",
   "metadata": {},
   "source": [
    "# 修改字符串\n",
    "Python 有一组可用于字符串的内置方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d36bbded",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HELLO, WORLD!\n"
     ]
    }
   ],
   "source": [
    "# 大写\n",
    "a = \"Hello, World!\"\n",
    "print(a.upper())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "7df03e7a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello, world!\n"
     ]
    }
   ],
   "source": [
    "# 小写\n",
    "a = \"Hello, World!\"\n",
    "print(a.lower())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d1315669",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello,     World!\n"
     ]
    }
   ],
   "source": [
    "# 删除前后空格\n",
    "a = \"         Hello,     World!         \"\n",
    "print(a.strip())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "1528c7bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jello, World!\n"
     ]
    }
   ],
   "source": [
    "# 替换\n",
    "a = \"Hello, World!\"\n",
    "print(a.replace(\"H\", \"J\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "72ed4a28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Hello', ' World!']\n"
     ]
    }
   ],
   "source": [
    "# 拆分字符串\n",
    "a = \"Hello, World!\"\n",
    "print(a.split(\",\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37a39687",
   "metadata": {},
   "source": [
    "# 字符串连接\n",
    "要连接或组合两个字符串，您可以使用 + 运算符。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "42dce5c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HelloWorld\n"
     ]
    }
   ],
   "source": [
    "a = \"Hello\"\n",
    "b = \"World\"\n",
    "c = a + b\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "00383027",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World\n"
     ]
    }
   ],
   "source": [
    "a = \"Hello\"\n",
    "b = \"World\"\n",
    "c = a + \" \" + b\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e43d36c",
   "metadata": {},
   "source": [
    "# 字符串格式化\n",
    "正如我们在 Python 变量一章中学到的，我们不能像这样组合字符串和数字："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a74d1ec9",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only concatenate str (not \"int\") to str",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-27-4d1b227cff41>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mage\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m36\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mtxt\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"My name is John, I am \"\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mage\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtxt\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
     ]
    }
   ],
   "source": [
    "age = 36\n",
    "txt = \"My name is John, I am \" + age\n",
    "print(txt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "704ae7dc",
   "metadata": {},
   "source": [
    "但是我们可以通过使用 format() 方法来组合字符串和数字！\n",
    "\n",
    "format() 方法接受传递的参数，格式化它们，并将它们放在占位符 {} 所在的字符串中："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "7ae72713",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "My name is John, and I am 36\n"
     ]
    }
   ],
   "source": [
    "age = 36\n",
    "txt = \"My name is John, and I am {}\"\n",
    "print(txt.format(age))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "609388f9",
   "metadata": {},
   "source": [
    "format() 方法接受无限数量的参数，并放置在各自的占位符中："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "72bf671d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I want 3 pieces of item number 567 for 49.00 dollars.\n"
     ]
    }
   ],
   "source": [
    "quantity = 3\n",
    "itemno = 567\n",
    "price = 49\n",
    "myorder = \"I want {} pieces of item number {} for {:.2f} dollars.\"\n",
    "print(myorder.format(quantity, itemno, price))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d01eea97",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I want to pay 49.95 dollars for 3 pieces of item 567.00.\n"
     ]
    }
   ],
   "source": [
    "quantity = 3\n",
    "itemno = 567\n",
    "price = 49.95\n",
    "myorder = \"I want to pay {2} dollars for {0} pieces of item {1:.2f}.\"\n",
    "print(myorder.format(quantity, itemno, price))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ea00a348",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I have a Ford, it is a Mustang.\n"
     ]
    }
   ],
   "source": [
    "myorder = \"I have a {carname}, it is a {model}.\"\n",
    "print(myorder.format(carname = \"Ford\", model = \"Mustang\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a1c5878",
   "metadata": {},
   "source": [
    "# 转义字符\n",
    "要在字符串中插入非法字符，请使用转义字符。\n",
    "\n",
    "转义字符是反斜杠 \\ 后跟要插入的字符。\n",
    "\n",
    "非法字符的一个例子是被双引号包围的字符串中的双引号："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "df761b2d",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-32-56cdf4283a8e>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-32-56cdf4283a8e>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    txt = \"We are the so-called \"Vikings\" from the north.\"\u001b[0m\n\u001b[1;37m                                       ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "txt = \"We are the so-called \"Vikings\" from the north.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "d50a6aee",
   "metadata": {},
   "outputs": [],
   "source": [
    "txt = \"We are the so-called \\\"Vikings\\\" from the north.\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90cdf4ef",
   "metadata": {},
   "source": [
    "<table>\n",
    "<tbody><tr>\n",
    "  <th>代码</th>\n",
    "  <th>结果</th>\n",
    "  <th>代码</th>\n",
    "  <th>结果</th>\n",
    "  <th>代码</th>\n",
    "  <th>结果</th>\n",
    "</tr>\n",
    "<tr>\n",
    "  <td>\\'</td>\n",
    "  <td>Single Quote</td>\n",
    "  <td>\\\\</td>\n",
    "  <td>Backslash</td>\n",
    "  <td>\\n</td>\n",
    "  <td>New Line</td>\n",
    "</tr>\n",
    "<tr>\n",
    "  <td>\\r</td>\n",
    "  <td>Carriage Return</td>\n",
    "  <td>\\t</td>\n",
    "  <td>Tab</td>\n",
    "  <td>\\b</td>\n",
    "  <td>Backspace</td>\n",
    "</tr>\n",
    "<tr>\n",
    "  <td>\\f</td>\n",
    "  <td>Form Feed</td>\n",
    "  <td>\\ooo</td>\n",
    "  <td>八进制值</td>\n",
    "  <td>\\xhh</td>\n",
    "  <td>十六进制值</td>\n",
    "</tr>\n",
    "</tbody></table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "463e7e63",
   "metadata": {},
   "source": [
    "# 字符串方法\n",
    "<table>\n",
    "<tr><th>方法</th><th>说明</th><th>方法</th><th>说明</th></tr>\n",
    "<tr><td>capitalize</td><td> 将第一个字符转换为大写</td><td>casefold</td><td> 将字符串转换为小写</td></tr>\n",
    "<tr><td>center</td><td> 返回一个居中的字符串</td><td>count</td><td> 返回指定值在字符串中出现的次数</td></tr>\n",
    "<tr><td>encode</td><td> 返回字符串的编码版本</td><td>Endswith</td><td> 如果字符串以指定值结尾，则返回 true</td></tr>\n",
    "<tr><td>expandtabs</td><td> 设置字符串的制表符大小</td><td>find</td><td> 在字符串中搜索指定值并返回找到它的位置</td></tr>\n",
    "<tr><td>format</td><td> 格式化字符串中的指定值</td><td>format_map</td><td> 格式化字符串中的指定值</td></tr>\n",
    "<tr><td>index</td><td> 在字符串中搜索指定值并返回找到它的位置</td><td>isalnum</td><td> 如果字符串中的所有字符都是字母数字，则返回 True</td></tr>\n",
    "<tr><td>isalpha</td><td> 如果字符串中的所有字符都在字母表中，则返回 True</td><td>isdecimal</td><td> 如果字符串中的所有字符都是小数，则返回 True</td></tr>\n",
    "<tr><td>isdigit</td><td> 如果字符串中的所有字符都是数字，则返回 True</td><td>isidentifier</td><td> 如果字符串是标识符，则返回 True</td></tr>\n",
    "<tr><td>islower</td><td> 如果字符串中的所有字符都是小写，则返回 True</td><td>isnumeric</td><td> 如果字符串中的所有字符都是数字，则返回 True</td></tr>\n",
    "<tr><td>isprintable</td><td> 如果字符串中的所有字符都可打印，则返回 True</td><td>isspace</td><td> 如果字符串中的所有字符都是空格，则返回 True</td></tr>\n",
    "<tr><td>istitle</td><td> 如果字符串符合标题规则，则返回 True</td><td>isupper</td><td> 如果字符串中的所有字符都是大写，则返回 True</td></tr>\n",
    "<tr><td>join</td><td> 将可迭代的元素连接到字符串的末尾</td><td>ljust</td><td> 返回字符串的左对齐版本</td></tr>\n",
    "<tr><td>lower</td><td> 将字符串转换为小写</td><td>lstrip</td><td> 返回字符串的左修剪版本</td></tr>\n",
    "<tr><td>maketrans</td><td> 返回要在翻译中使用的翻译表</td><td>partition</td><td> 返回一个元组，其中字符串被分成三部分</td></tr>\n",
    "<tr><td>replace</td><td> 返回指定值替换为指定值的字符串</td><td>rfind</td><td> 在字符串中搜索指定值并返回找到它的最后位置</td></tr>\n",
    "<tr><td>rindex</td><td> 在字符串中搜索指定值并返回找到它的最后位置</td><td>rjust</td><td> 返回字符串的右对齐版本</td></tr>\n",
    "<tr><td>rpartition</td><td> 返回一个元组，其中字符串被分成三部分</td><td>rsplit</td><td> 在指定的分隔符处拆分字符串，并返回一个列表</td></tr>\n",
    "<tr><td>rstrip</td><td> 返回字符串的正确修剪版本</td><td>split</td><td> 在指定的分隔符处拆分字符串，并返回一个列表</td></tr>\n",
    "<tr><td>splitlines</td><td> 在换行符处拆分字符串并返回一个列表</td><td>startswith</td><td> 如果字符串以指定值开头，则返回 true</td></tr>\n",
    "<tr><td>strip</td><td> 返回字符串的修剪版本</td><td>swapcase</td><td> 交换大小写，小写变为大写，反之亦然</td></tr>\n",
    "<tr><td>title</td><td> 将每个单词的第一个字符转换为大写</td><td>translate</td><td> 返回翻译后的字符串</td></tr>\n",
    "<tr><td>upper</td><td> 将字符串转换为大写</td><td>zfill</td><td> 在字符串的开头用指定数量的 0 值填充</td></tr>\n",
    "</table>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
